#include "../server.hpp"


class EchoServer
{
private:
    TcpServer _server;
private:
    void OnConnected(const PtrConnection& conn)
    {
        DBG_LOG("new connection:%p", conn.get());
    }
    void OnClosed(const PtrConnection& conn)
    {
        DBG_LOG("close connection:%p", conn.get());
    }
    void OnMessage(const PtrConnection& conn, Buffer* buf)
    { 
        conn->Send(buf->ReadPosition(), buf->ReadableSize());
        buf->MoveReadOffset(buf->ReadableSize());

        conn->ShutDown();
    }
public:
    EchoServer(uint16_t port):_server(port)
    {
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetConnectedCallback(bind(&EchoServer::OnConnected, this, placeholders::_1));
        _server.SetMessageCallback(bind(&EchoServer::OnMessage, this, placeholders::_1, placeholders::_2));
        _server.SetClosedCallback(bind(&EchoServer::OnClosed, this, placeholders::_1));
    }
    void Start()
    {
        _server.StartServer();
    }

};